/**
 * 
 */
package com.demo2do.boyle.backend.service.report.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.backend.service.impl.DataServiceImpl;
import com.demo2do.boyle.backend.service.report.AdvertisementStatisticsService;
import com.demo2do.boyle.common.entity.report.AdvertisementStatisticsDto;
import com.demo2do.core.persistence.GenericJdbcSupport;

/**
 * @author Jason.Y
 *
 */
@Service("advertisementStatisticsService")
public class AdvertisementStatisticsServiceImpl extends DataServiceImpl<AdvertisementStatisticsDto> implements AdvertisementStatisticsService {

	@Autowired 
	private GenericJdbcSupport genericJdbcSupport;
	
	/* (non-Javadoc)
     * @see com.demo2do.boyle.backend.service.DataService#listByPage(Class<StatisticsConverge> clazz, Page page, Integer id)
     */
	@Override
	public Map<String, Object> queryForObject(Integer advertisementId) {
		
		String sentence = "SELECT sum(man) man, sum(woman) woman FROM advertisement_statistics WHERE type = :type AND advertisement_id = :advertisementId ";
    	
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("type", 4);
		parameters.put("advertisementId", advertisementId);
		
		List<Map<String,Object>> result = genericJdbcSupport.queryForList(sentence, parameters);
        
        return result.size() == 0 ? null : result.get(0);
    }

	@Override
	public List<Map<String, Object>> listByAge(Integer advertisementId) {
		
		String sentence = "SELECT tag, sum(man) man, sum(woman) woman FROM advertisement_statistics WHERE type = :type AND advertisement_id = :advertisementId GROUP BY tag ORDER BY tag ASC ";
    	
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("type", 3);
		parameters.put("advertisementId", advertisementId);
		
		List<Map<String,Object>> result = genericJdbcSupport.queryForList(sentence, parameters);
        
        return result.size() == 0 ? null : result;
    }

}
